/*
 * Copyright (C) 2008-2010 TrinityCore <http://www.trinitycore.org/>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "ScriptPCH.h"
#include "violet_hold.h"

enum Spells
{
    SPELL_SHROUD_OF_DARKNESS                     = 54524, 
    H_SPELL_SHROUD_OF_DARKNESS                   = 59745, 
    SPELL_SUMMON_VOID_SENTRY                     = 54369, 
    SPELL_VOID_SHIFT                             = 54361, 
    H_SPELL_VOID_SHIFT                           = 59743,
    SPELL_VOID_SHIFTED                           = 54343,

    SPELL_ZURAMAT_ADD_2                          = 54342, 
    H_SPELL_ZURAMAT_ADD_2                        = 59747 
};

enum Yells
{
    SAY_AGGRO                                    = -1608037, 
    SAY_SLAY_1                                   = -1608038, 
    SAY_SLAY_2                                   = -1608039, 
    SAY_SLAY_3                                   = -1608040, 
    SAY_DEATH                                    = -1608041, 
    SAY_SPAWN                                    = -1608042, 
    SAY_SHIELD                                   = -1608043, 
    SAY_WHISPER                                  = -1608044 
};

enum ModelID
{
    MODEL_VOID_SENTRY                            = 23767
};

enum eAchievement
{
    ACHIEV_A_VOID_DANCE                          = 2153
};

class boss_zuramat : public CreatureScript
{
public:
    boss_zuramat() : CreatureScript("boss_zuramat") { }

    CreatureAI* GetAI(Creature* pCreature) const
    {
        return new boss_zuramatAI (pCreature);
    }

    struct boss_zuramatAI : public ScriptedAI
    {
        boss_zuramatAI(Creature *c) : ScriptedAI(c)
        {
            pInstance = c->GetInstanceScript();
        }

        InstanceScript* pInstance;

        uint32 uiVoidShift_Timer;
        uint32 uiSummonVoid_Timer;
        uint32 uiShroudOfDarkness_Timer;

        void Reset()
        {
            if (pInstance)
            {
                if (pInstance->GetData(DATA_WAVE_COUNT) == 6)
                    pInstance->SetData(DATA_1ST_BOSS_EVENT, NOT_STARTED);
                else if (pInstance->GetData(DATA_WAVE_COUNT) == 12)
                    pInstance->SetData(DATA_2ND_BOSS_EVENT, NOT_STARTED);
					
                pInstance->SetData(DATA_VOID_SENTRY, 1);
            }

            uiShroudOfDarkness_Timer = 22000; 
            uiVoidShift_Timer = 15000; 
            uiSummonVoid_Timer = 12000;
        }

        void AttackStart(Unit* pWho)
        {
            if (me->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_OOC_NOT_ATTACKABLE) || me->HasFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_NON_ATTACKABLE))
                return;

            if (me->Attack(pWho, true))
            {
                me->AddThreat(pWho, 0.0f);
                me->SetInCombatWith(pWho);
                pWho->SetInCombatWith(me);
                DoStartMovement(pWho);
            }
        }

        void EnterCombat(Unit* /*who*/)
        {
            DoScriptText(SAY_AGGRO, me);
            if (pInstance)
            {
                if (GameObject *pDoor = pInstance->instance->GetGameObject(pInstance->GetData64(DATA_ZURAMAT_CELL)))
                    if (pDoor->GetGoState() == GO_STATE_READY)
                    {
                        EnterEvadeMode();
                        return;
                    }
                if (pInstance->GetData(DATA_WAVE_COUNT) == 6)
                    pInstance->SetData(DATA_1ST_BOSS_EVENT, IN_PROGRESS);
                else if (pInstance->GetData(DATA_WAVE_COUNT) == 12)
                    pInstance->SetData(DATA_2ND_BOSS_EVENT, IN_PROGRESS);
            }
        }

        void UpdateAI(const uint32 uiDiff) 
        {
            //Return since we have no target
            if (!UpdateVictim())
                return;

            if (uiSummonVoid_Timer <= uiDiff)
            {
                DoCast(me->getVictim(), SPELL_SUMMON_VOID_SENTRY, false);
                uiSummonVoid_Timer = urand(8000,10000); 
            } else uiSummonVoid_Timer -= uiDiff; 

            if (uiVoidShift_Timer <= uiDiff) 
            {
                 if (Unit* pUnit = SelectUnit(SELECT_TARGET_RANDOM, 0))
                    DoCast(pUnit, SPELL_VOID_SHIFT);
                uiVoidShift_Timer = urand(12000,14000); 
            } else uiVoidShift_Timer -= uiDiff; 

            if (uiShroudOfDarkness_Timer <= uiDiff)
            {
                DoCast(me->getVictim(), SPELL_SHROUD_OF_DARKNESS);
                uiShroudOfDarkness_Timer = urand(14000,15500); 
            } else uiShroudOfDarkness_Timer -= uiDiff; 

            DoMeleeAttackIfReady();
        }

        void JustDied(Unit* /*killer*/)
        {
            DoScriptText(SAY_DEATH, me);

            if (pInstance)
            {
                if (pInstance->GetData(DATA_VOID_SENTRY))
                    pInstance->DoCompleteAchievement(ACHIEV_A_VOID_DANCE);

                if (pInstance->GetData(DATA_WAVE_COUNT) == 6)
                {
                    pInstance->SetData(DATA_1ST_BOSS_EVENT, DONE);
                    pInstance->SetData(DATA_WAVE_COUNT, 7);
                }
                else if (pInstance->GetData(DATA_WAVE_COUNT) == 12)
                {
                    pInstance->SetData(DATA_2ND_BOSS_EVENT, DONE);
                    pInstance->SetData(DATA_WAVE_COUNT, 13);
                }
            }
        }

        void KilledUnit(Unit * victim)
        {
            if (victim == me)
                return;

            DoScriptText(RAND(SAY_SLAY_1, SAY_SLAY_2, SAY_SLAY_3), me);
        }

        void JustSummoned(Creature* summon)
        {
            summon->setFaction(14);
            summon->SetFlag(UNIT_FIELD_FLAGS, UNIT_FLAG_DISABLE_MOVE);
            summon->SetDisplayId(MODEL_VOID_SENTRY);
            summon->AI()->DoCastAOE(SPELL_ZURAMAT_ADD_2);
            summon->SetPhaseMask(17, true);
        }
    }; 
};

class mob_void_sentry : public CreatureScript
{
public:
    mob_void_sentry() : CreatureScript("mob_void_sentry") { }

    CreatureAI* GetAI(Creature* pCreature) const
    {
        return new mob_void_sentryAI (pCreature);
    }

    struct mob_void_sentryAI : public ScriptedAI
    {
        mob_void_sentryAI(Creature *c) : ScriptedAI(c)
        {
            pInstance = c->GetInstanceScript();
        }

        InstanceScript *pInstance;

        void AttackStart(Unit * pWho)
        {
            if (!pWho)
                return;

            me->AddThreat(pWho, 0.0f);
            me->SetInCombatWith(pWho);
            pWho->SetInCombatWith(me);
        }

        void JustDied(Unit * /*killer*/)
        {
            if (pInstance)
                pInstance->SetData(DATA_VOID_SENTRY, 0);
        }
    };
};

void AddSC_boss_zuramat()
{
    new boss_zuramat();
	new mob_void_sentry();
}
